package com.decodeUtil;

import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.show.api.util.StringUtils;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：yzf_jiao
 * 类描述：解密脚本
 * 创建人：罗浩
 * 创建时间：2020/5/7 11:22
 * 修改人：Administrator
 * 修改时间：2020/5/7 11:22
 * 修改备注：解密成功后生成类似HKL4RY4W_20200512_M_Decode.txt文件，直接下边命令导入桌面
 * adb pull sdcard/yzflog/mobile/HKL4RY4W_20200512_M_Decode.txt C:\Users\Administrator\Desktop
 */
@RunWith(AndroidJUnit4.class)
public class DecodeHelper {
    private static String SDPATH = "/sdcard/yzflog";
    String project = "/mobile/";
    String logPath = SDPATH + project;
    String fileName = "HKL4RY4W_20200512_M";//要解密的文件名

    @Test
    public void decodeMain() throws Exception {
        String DecodePath = logPath + fileName;
        List<String> msg = txt2String(DecodePath + ".log");
        Log.i("====Log解密====", "文件--" + msg);
        String DecodeWritePath = DecodePath + "_Decode.log";
        File decodeLogFile = createSDFile(DecodeWritePath);
        for (String s : msg) {
            if (!StringUtils.isEmpty(s)) {
                String decode_string = DesUtil.decode(s);
                Log.i("====Log解密====", "文件--" + decode_string);
                addWriteSDFile(true, decode_string + "\n", decodeLogFile);
            }
        }
    }

    /**
     * 读取SD卡中文本文件
     *
     * @return
     */
    public String readSDFile(String sdpath) {
        Log.i("====Log解密====", "开始读取加密文件：" + sdpath);
        StringBuffer sb = new StringBuffer();
        File file = new File(sdpath);
        try {
            FileInputStream fis = new FileInputStream(file);
            int c;
            while ((c = fis.read()) != -1) {
                sb.append((char) c);
            }
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 在SD卡上创建文件
     *
     * @throws IOException
     */
    public static File createSDFile(String fileName) throws IOException {
        File file = new File(fileName);
        Log.i("====Log解密====", "生成文件：" + file.getPath());
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    /**
     * 写入内容到SD卡中的txt文本中
     *
     * @param isAdd    是否在文件中追加
     * @param str      储存数据
     * @param fileName 文件名
     */
    public static void addWriteSDFile(boolean isAdd, String str, File fileName) {
        FileOutputStream fos = null;//FileOutputStream会自动调用底层的close()方法，不用关闭
        BufferedWriter bw = null;
        try {
            fos = new FileOutputStream(fileName.getPath(), isAdd);//这里的第二个参数isAdd代表追加还是覆盖，true为追加，flase为覆盖
            bw = new BufferedWriter(new OutputStreamWriter(fos, Charset.forName("utf-8")));
            bw.write(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();//关闭缓冲流
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<String> txt2String(String file) {
        File file1 = new File(file);
        List<String> list = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file1));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                s = s.replaceAll("[\\s*\t\n\r]", "");
                list.add(s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
